package main

import (
	"errors"
	"next-ppt/method"
	"next-ppt/sql"
	"next-ppt/src"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func main() {
	var adminif sql.User
	err := method.Db.Where("id = ? AND type = ? ", 1, "admin").First(&adminif).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		pwd, err := method.HashPwd(method.Config.Admin.Password)
		if err != nil {
			method.Exit("HashPwd", err)
		}
		admin := sql.User{
			Name:     method.Config.Admin.Name,
			Password: pwd,
			Type:     "admin",
			Phone:    method.Config.Admin.Phone,
			Icon:     "/img/0.png",
			Time:     time.Now().Unix(),
			Info:     "管理员",
			Email:    method.Config.Admin.Email,
		}
		method.Db.Create(&admin)
	}
	//gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.Static("/app", "./app")
	r.Use(Cors())
	api := r.Group("/api")
	api.Use(ApiUser())
	api.POST("/register", src.UserRegister)
	api.POST("/login", src.UserLogin)
	api.POST("/changeImg", src.ChangeImg)
	api.GET("/userImg/:id", src.UserImg)
	api.POST("/userInfo", src.UserInfo)
	api.POST("/uploadPpt", src.UploadPpt)
	api.POST("/cancelUploadPpt", src.CancelUploadPpt)
	api.POST("/uploadPptFile", src.UploadPptFile)
	api.POST("/delPpt", src.DelPpt)
	api.POST("/createPptDownloadKey", src.CreatePptDownloadKey)
	api.GET("/downloadPpt/:id", src.DownloadPpt)
	api.POST("/getPptList/:page", src.GetPptList)
	api.POST("uploadPptImg", src.UploadPptIcon)
	api.GET("/pptImg/:id", src.PptImg)
	api.POST("/getPptInfo", src.PptInfo)
	api.POST("/getPptReview", src.GetPptReview)
	api.POST("/getUserPptList", src.GetUserPptList)
	r.Run(":" + method.Config.Server.Port)
}
